﻿using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace BookShop
{
    /// <summary>
    /// Controller class for Message
    /// </summary>
    public partial class MessageController
    {
        public static int GetNewMessagesCount(string username)
        {
            int count = new Select().From<Message>()
                           .Where(Message.ReceiverColumn).IsEqualTo(username)
                           .And(Message.StatusColumn).IsEqualTo("new")
                           .GetRecordCount();
            return count;
        }

        public static void SetMessageDone(int msgId)
        {
            Message msg = new Message(msgId);
            msg.Status = "done";
            msg.Save();
        }

        public MessageCollection FetchByReceiver(object receiver)
        {
            MessageCollection coll = new MessageCollection();
            SqlQuery query = new Select(Message.MsgIDColumn, Message.TitleColumn, Message.SenderColumn, Message.SourceColumn, Message.TypeColumn)
                            .From<Message>()
                            .Where(Message.ReceiverColumn).IsEqualTo(receiver)
                            .And(Message.StatusColumn).IsEqualTo("new");
            coll.LoadAndCloseReader(query.ExecuteReader());
            return coll;
        }
    }
}
